﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Apewer.Source
{

    /// <summary>表示在执行 SQL 语句执行过程中发生的错误。</summary>
    [Serializable]
    public sealed class SqlException : Exception
    {

        const string EmptyMessage = "（无消息）";

        string _msg = null;
        string _sql = null;

        /// <summary>获取描述当前异常的消息。</summary>
        public override string Message { get => _msg; }

        /// <summary>获取引发异常的 SQL 语句。</summary>
        public string Statement { get => _sql; }

        /// <summary>初始化 <see cref="SqlException"/> 类的新实例。</summary>
        /// <param name="message">描述当前异常的消息。</param>
        /// <param name="statement">附带 SQL 语句。</param>
        public SqlException(string message, string statement = null)
        {
            _msg = string.IsNullOrEmpty(message) ? EmptyMessage : message;

            _sql = statement;
        }

        /// <summary>初始化 <see cref="SqlException"/> 类的新实例。</summary>
        /// <param name="query">用于获取消息的查询结果。</param>
        /// <param name="statement">附带 SQL 语句。</param>
        public SqlException(IQuery query, string statement = null)
        {
            if (query == null)
            {
                _msg = "查询结果实例无效。";
                _sql = statement;
                return;
            }

            _msg = query.Message;
            if (string.IsNullOrEmpty(_msg)) _msg = EmptyMessage;

            _sql = statement;
        }

        /// <summary>初始化 <see cref="SqlException"/> 类的新实例。</summary>
        /// <param name="execute">用于获取消息的执行结果。</param>
        /// <param name="statement">附带 SQL 语句。</param>
        public SqlException(IExecute execute, string statement = null)
        {
            if (execute == null)
            {
                _msg = "执行结果实例无效。";
                return;
            }

            _msg = execute.Message;
            if (string.IsNullOrEmpty(_msg)) _msg = EmptyMessage;

            _sql = statement;
        }

    }

}
